Applying Reversed Patches

Contents|Index|Previous|Next

Applying Reversed Patches

Sometimes people run diff with the new file first instead of second. This creates a diff that is reversed. To apply such patches, give patch the '-R' or'--reverse' option. patch then attempts to swap each hunk around before applying it. Rejects come out in the swapped format. The '-R' option does not work with ed scripts because there is too little information in them to reconstruct the reverse operation. Often patch can guess that the patch is reversed. If the first hunk of a patch fails, patch reverses the hunk to see if it can apply it that way. If it can, patch asks you if you want to have the '-R' option set; if it can't, patch continues to apply the patch normally. This method cannot detect a reversed patch if it is a normal diff and the first command is an append (which should have been a delete) since appends always succeed, because a null context matches anywhere. But most patches add or change lines rather than delete them, so most reversed normal diffs begin with a delete, which fails, and patch notices.

If you apply a patch that you have already applied, patch thinks it is a reversed patch and offers to un-apply the patch. This could be construed as a feature. If you did this inadvertently and you don't want to un-apply the patch, just answer 'n' to this offer and to the subsequent "apply anyway" question—or use the keystroke sequence, C-c, to kill the patch process. See also Applying Patches with Changed White Space and Helping patch Find Inexact Matches for more details.